package com.syqc.monitor.ui.historyline

import android.view.View
import android.widget.ImageView
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.syqc.comlib.base.LazyFragment
import com.syqc.comlib.databinding.IncludeRecyclerMathMBinding
import com.syqc.comlib.utils.loadGif
import com.syqc.comlib.utils.startActivityTo
import com.syqc.entity.HistoryLine
import com.syqc.monitor.R
import com.syqc.monitor.ui.realalarm.MediaInfoActivity
import com.syqc.utils.Keys


/**
 *@Author Jasper
 *@Time   2020/8/12
 *@Desc   历史轨迹--报警
 */
class AlarmLineFragment :
    LazyFragment<IncludeRecyclerMathMBinding>(IncludeRecyclerMathMBinding::inflate) {

    private val mActivityVM by activityViewModels<LineInfoViewModel>()
    private val mAdapter by lazy { DataAdapter<HistoryLine.AlarmData>(1) }
    override fun initView(view: View) {
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
    }

    //空布局
    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            binding.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(requireContext(), R.mipmap.ic_empty)
        view
    }

    override fun initData() {
        mActivityVM.data.observe(this) {

            binding.recyclerView.adapter = mAdapter
            if (it.alarmData.isNullOrEmpty()) {
                mAdapter.setEmptyView(emptyView)
                return@observe
            }
            val list: MutableList<HistoryLine.AlarmData> = ArrayList()
            list.addAll(it.alarmData)
            list.forEach { item ->
                item.mediaDetail?.forEach { media ->
                    media.mediaList.forEach {info->
                        info.teamName=mActivityVM.teamName
                    }
                }
            }
            mAdapter.setList(list)
        }

    }

    override fun initEvent() {
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            startActivityTo<MediaInfoActivity>(requireContext()) {
                putExtra(Keys.INTENT_TITLE, mActivityVM.carName)
                putExtra(Keys.INTENT_CAR_ID, mActivityVM.carId)
                putParcelableArrayListExtra(Keys.INTENT_DATA, mAdapter.data[position].mediaDetail)
            }
        }
    }
}